import { reactive } from 'vue'
import { isArray, replace, endsWith, trimEnd } from 'lodash-es'
import store from '../index'
import api from '@/utils/page/api'
import { getPrivilegesApi, getSearchDataApi, getTableDataApi } from '@/api/common'
import search from '@/utils/page/search' // 每个页面搜索type
import { Page } from '@/types/storeModule'

const state = {
    count: 1 // 列表依赖搜索数据
}
const actions = {
    getPrivileges: async ({}, payload: string) => {
        // console.log('payload--', payload);

        const privileges = store.state[payload].privileges
        // console.log('privileges--', privileges);
        if (isArray(privileges) && privileges.length > 0) {
            const params = {
                privileges: privileges.join(',')
            }
            const data = await getPrivilegesApi(params)
            // console.log('----', data);      
            store.commit(`${payload}/updateRoles`, data)
        }
    },
    getSearchData: async ({ commit, state }, payload: string) => {
        const searchType = search[window.location.pathname]
        if (!searchType) {
            setTimeout(() => {
                commit('updateCount', --state.count)
            }, 0)
            return
        }
        store.commit(`${payload}/updateLoading`, true)
        const params = store.state[payload].params
        // const data: any = await getSearchData(searchType, params)

        // 配置默认值 start
        const options = store.state[payload].params.options
        // data.forEach((item) => {
        //   if (item.defaultValue) {
        //     options[item.paramName] = item.defaultValue
        //   }
        // })
        // Object.assign(options, data)
        store.commit(`${payload}/updateParams`, { options: options })
        // 配置默认值 end
        // store.commit(`${payload}/updateSearchData`, data)
        commit('updateCount', --state.count)
    },

    getTableData: async ({}, payload: string) => {
        store.commit(`${payload}/updateLoading`, true)
        let pathName = window.location.pathname
        let baseUrl = import.meta.env.VITE_BASE_URL
        if(baseUrl !== '/'){
            if(endsWith(baseUrl, '/')){
                baseUrl = trimEnd(baseUrl, '/')
            }
            pathName = replace(pathName, baseUrl, '')
        }
        const apiUrl = api[pathName]
        const params = store.state[payload].params
        // console.log('pathname1----', window.location.pathname);
        // console.log('pathname2----', pathName);
        // console.log('payload----', payload);
        // console.log('apiUrl----', apiUrl);
        // console.log('params----', params);
        // console.log('params--params--', params);

        const data: any = await getTableDataApi(apiUrl, params)
        store.commit(`${payload}/updateLoading`, false)
        const page = reactive<Page>({
            currentPage: data.currentPage,
            pageSize: data.pageSize,
            totalCount: data.totalCount,
            sortField: data.sortField,
            order: data.order
        })
        store.commit(`${payload}/updatePage`, page as Page)
        store.commit(`${payload}/updateTableData`, data.result ? data.result : (data as []))
    }
}

const mutations = {
    updateCount(state, payload: number) {
        state.count = payload
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
